/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.dao;

import br.com.deployment.daogenerico.DaoGenerico;
import br.com.deployment.entidade.registroimovel.GeoRef;
import br.com.deployment.interfaces.MapDaoIn;
import java.lang.reflect.Field;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class MapDao  extends DaoGenerico<GeoRef> implements MapDaoIn{


    public MapDao() {
        super(GeoRef.class);
    }
    
    public List<GeoRef> getGeoRef(Class classe, int id){
        String geoRef = "";
        try {
            Class cls = Class.forName(classe.getName());
             for (Field field : cls.getDeclaredFields()) {
                if (field.getName().contains("geoRef")) {
                   geoRef = field.getName();
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MapDao.class.getName()).log(Level.SEVERE, null, ex);
        }
         
        String sql = "Select a from "+classe.getSimpleName()+" c JOIN"
                + " c."+geoRef+" a where "
                + " c."+ (classe.getSimpleName().equalsIgnoreCase("Cidade") ? "cod_cidade":"id") +" = :id "
                + " ORDER BY a.id ASC ";
        Query query = getEm().createQuery(sql);
        query.setParameter("id", id);
        return query.getResultList();
    }
}
